# *-*- coding:utf-8 -*-
# @Author  : Eliza
# @Time    : 2021/7/18 22:28
# @Software : RubyMine

puts File.superclass #IO --
full_name = 'E:\Code\RUBY\RubyL\src\base\CoreModules\FileL.rb'
puts File.basename(full_name) #文件名全称
puts File.basename(full_name, '.rb') #文件名
puts File.extname(full_name) #扩展名
puts File.dirname(full_name) #路径目录 E:\Code\RUBY\RubyL\src\base\CoreModules
puts File.join('home', 'maizi', 'code ') #路径拼接 home/maizi/code
puts File.expand_path("~/ruby") #C:/Users/ygree/ruby

puts "---navigate and examine directories and files"
puts Dir.pwd #当前工作路径 E:\Code\RUBY\RubyL\src\base\CoreModules
# puts Dir.chdir('/') #0 更改路径
puts Dir.pwd

puts Dir.entries('.') #当前 路径下所有文件和文件夹
=begin
.
..
DateAndTime.rb
FileL.rb
=end

puts '--------'
puts Dir['*.rb'] #Error.rb 以.rb结尾的
puts File.exist?('Error.rb') #false
puts File.exist?('FileL.rb') #false

puts "-----------"
puts File.directory?('CoreModules') #false
puts File.directory?(full_name) #false
puts File.file?(full_name) #true
puts File.size(full_name) #1141
puts "----iterating files--"
puts Dir['CoreModules/*.rb']
# puts Dir.glob('CoreModules/*.rb') { |f| load f }
puts "-----文件目录操作-------"
##mainpulating files
# Dir.mkdir( 'tmp')
# Dir.rmdir( 'tmp')
# File.rename( 'test' , 'new_test')
# File.delete(file_name)
# File.symlink( 'test' , 'old_test' )
puts "-----read and write steams------"
# File class is a subclass of I0 -- standard input and standard output
# r r+ w w+ a

def openFile(file_path)
  begin
    f = File.open(file_path, 'r')
    puts f.readlines #readlines自动关闭
    puts "----------"
    File.open(file_path, 'r') do |value|
      #value 即该文件
      p value.readlines[0]
      #["AAAAAAAAAA\n", "VVVVV\n", "BBBBBB\n"]
      # "AAAAAAAAAA\n"
    end
  rescue
    puts "--something wrong--"
  ensure
    puts "------over--"
    f.close if f
  end
end

file_path = "E:\\Code\\RUBY\\RubyL\\src\\base\\CoreModules\\text.txt"
openFile(file_path)
=begin
AAAAAAAAAA
VVVVV
BBBBBB
=end

puts "-------File---"
puts File.readlines(file_path) #automatically close the file
=begin
  AAAAAAAAAA
  VVVVV
  BBBBBB
=end

puts "----------"

puts File.read(file_path)
puts "----------"
File.open(file_path, "w") do |value|
  #覆盖重写
  value << "ooooooooo"
  value << "tttttttttt"
  value.puts 'llllll' #自带换行符，向下换行
  value.puts 'wwwwww'
end
